package com.prp.struts.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.prp.dao.Grade;
import com.prp.dao.Item;
import com.prp.dao.ItemDAO;
import com.prp.dao.ShowCodeDAO;
import com.prp.service.ServiceFactory;

public class FileManageAction extends ActionSupport {

private String username;
	
	private int itemID; 
	private Item item;
	
	private File file;
	private String year;
	private String fileFileName;
	private String fileContentType;
	private List countryList;
	private int new_stat;
	private boolean isSUrl;
	
	private String next;
	
	private String up_success;
	private String up_fail_message;
	private boolean isCollect;

	
	
	
	public void setFile(File a){
		file = a;
	}
	public File getFile(){
		return file;
	}
	public void setFileFileName(String a){
		fileFileName = a;
	}
	public String getFileFileName(){
		return fileFileName;
	}
	
	public void setFileContentType(String a){
		fileContentType = a;
	}
	public String getFileContentType(){
		return fileContentType;
	}
	
	public void setItem(Item a){
		item = a;
	}
	public Item getItem(){
		return item;
	}
	
	public void setUsername(String a){
		username = a;
	}
	public String getUsername(){
		return username;
	}
	
	/**
	 * @return
	 */
	public String execute() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
	public String editFile() throws ParseException{
		if (!year.equals(""))
			item.setPublishTime((new SimpleDateFormat("yyyy").parse(year)));
		
		ServiceFactory.getFileManage().editFile(item, file, fileFileName);
		
		if (next!= null && next.equals("show"))
			return "showinfo";
		
		return SUCCESS;
		
	}
	public void setItemID(int itemID) {
		this.itemID = itemID;
	}
	public int getItemID() {
		return itemID;
	}
	
	public String updateFile() throws ParseException{
		
		if (item == null)
		{
			return INPUT;
		}
		if (!year.equals(""))
			item.setPublishTime((new SimpleDateFormat("yyyy").parse(year)));
		
		ServiceFactory.getFileManage().updateFile(item, file, "", fileFileName);
		return SUCCESS;
	}
	
	public String updatePage()
	{
		return SUCCESS;
	}
	
	public String editPage()
	{
		item = ServiceFactory.getFileManage().getFileInfo(itemID);
		DateFormat format = new SimpleDateFormat("yyyy");
		try{
			year = format.format(item.getPublishTime());
		}
		catch(Exception e){
			
		}
		return SUCCESS;
	}
	public String showInfo()
	{
		item = ServiceFactory.getFileManage().getFileInfo(itemID);
		String s = item.getDataSource();
		String aaaa = next;
		new_stat = item.getState();
		isSUrl = s.contains("http://");
		
		/*
		 * view num +1 
		 */
		ItemDAO itemDao = new ItemDAO();
		itemDao.getSession().beginTransaction();
		

		item.setViewNum(item.getViewNum() + 1);		
		itemDao.attachDirty(item);		
		
		
		String u = (String) ActionContext.getContext().getSession().get("username");
		this.isCollect = ServiceFactory.getCollectService().isCollect(u, item);
					
		
		itemDao.getSession().getTransaction().commit();
		itemDao.getSession().close();
		// TODO Auto-generated method stub
		return "show";
	}
	
	public String switchItemStat()
	{
		String str = (String) ActionContext.getContext().getSession().get("username");
		if (str == null)
		{
			return SUCCESS;
		}
		ServiceFactory.getFileManage().switchItemStat(str, itemID, new_stat);
		return SUCCESS;
	}
	
	public String ajaxSwitItemStat() throws IOException{
		this.switchItemStat();
		HttpServletResponse response = ServletActionContext.getResponse();		
		response.setCharacterEncoding("UTF-8");    		       
		PrintWriter out = response.getWriter();
		
		HashMap map = new HashMap();
		map.put("item", itemID);
		map.put("new_stat", new_stat);
		
		JSONObject json = JSONObject.fromObject(map);
		out.print(json.toString());
		out.flush();
		out.close();
		
		
		return null;
	}
	public String countryList(){
		ShowCodeDAO d = new ShowCodeDAO();
		ItemDAO dao = new ItemDAO();
		String queryString = " from Item group by country";
		
		
		Query queryObject = dao.getSession().createQuery(queryString);


		Map s = ActionContext.getContext().getSession();
		
		this.countryList = queryObject.list();
		
		return SUCCESS;
	}
	public void setNew_stat(int new_stat) {
		this.new_stat = new_stat;
	}
	public int getNew_stat() {
		return new_stat;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getYear() {
		return year;
	}
	public void setIsSUrl(boolean isSUrl) {
		this.isSUrl = isSUrl;
	}
	public boolean getIsSUrl() {
		return isSUrl;
	}
	public void setCountryList(List countryList) {
		this.countryList = countryList;
	}
	public List getCountryList() {
		return countryList;
	}
	
	public void setUp_success(String up_success) {
		this.up_success = up_success;
	}
	public String getUp_success() {
		return up_success;
	}
	public void setUp_fail_message(String up_fail_message) {
		this.up_fail_message = up_fail_message;
	}
	public String getUp_fail_message() {
		return up_fail_message;
	}
	public void setNext(String next) {
		this.next = next;
	}
	public String getNext() {
		return next;
	}
	public void setIsCollect(boolean isCollect) {
		this.isCollect = isCollect;
	}
	public boolean getIsCollect() {
		return isCollect;
	}


}